package com.zxw.hotel.api;

import ch.qos.logback.core.util.StringUtil;
import com.wf.captcha.SpecCaptcha;
import com.zxw.hotel.common.JwtUtils;
import com.zxw.hotel.model.Account;
import com.zxw.hotel.service.AdminLoginService;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.jasypt.util.password.PasswordEncryptor;
import org.jasypt.util.password.StrongPasswordEncryptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping(value = "/api/v1/admins", produces = "application/json;charset=UTF-8")
public class AdminLoginApi {
    // 密码加密器
private static final PasswordEncryptor encryptor = new StrongPasswordEncryptor();
    @Value("${jwt.secret.key}")
    private String jwtsecret;
    private AdminLoginService adminLoginService;
  @Autowired
    public AdminLoginApi(AdminLoginService adminLoginService) {
        this.adminLoginService = adminLoginService;
    }


    @PostMapping("/login")
    public Map<String,Object> adminLogin(@RequestBody Account account,HttpSession session) {
        //校验
        if(!StringUtils.hasText(account.getCaptcha())){
            return Map.of("success", false,"error","验证码不可为空");
        }
        String captcha = (String) session.getAttribute("captcha");
        if(!captcha.equalsIgnoreCase(account.getCaptcha())){
            return Map.of("success", false,"error","验证码错误");
        }
       if(!StringUtils.hasText(account.getAdminName())){
           return Map.of("success", false,"error","用户名不可为空");
        }
        if(!StringUtils.hasText(account.getPassword())) {
            return Map.of("success", false, "error", "密码不可为空");
        }

        //登陆验证
        Account adminLogin = adminLoginService.adminLogin(account);

        if(adminLogin==null){
            return Map.of("success", false,"error","用户不存在");
        }
        //检查密码
        if(!encryptor.checkPassword(account.getPassword(), adminLogin.getPassword())) {
            return Map.of("success", false,"error","密码错误");
        }
        //颁发令牌
        Map<String,Object> data=Map.of("adminName",adminLogin.getAdminName());
        String jwt = JwtUtils.encode(adminLogin.getAdminName(), data, jwtsecret);
        return Map.of("success", true,"jwt",jwt);
    }
    //生成验证码
    @GetMapping("/captcha")
    public void captcha(HttpServletResponse resp, HttpSession session) throws IOException {
        //生成验证码
        SpecCaptcha cap = new SpecCaptcha(160, 30, 5);

        //设置响应头
        resp.setContentType("image/gif");
        resp.setHeader("Pragma", "No-cache");
        resp.setHeader("Cache-Control", "no-cache");
        resp.setDateHeader("Expires", 0L);

        //将答案存储到session中
        session.setAttribute("captcha", cap.text().toLowerCase());
        cap.out(resp.getOutputStream());
    }

}
